package com.zjw.zy.base;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/9/27 18:38
 */
public abstract class BaseViewGroup extends FrameLayout {

    private View mRootView;

    private boolean isInit = false;
    private boolean isAttrs = false;
    private boolean isLoadChildView = false;

    public BaseViewGroup(Context context) {
        super(context);
        init(context, this);
    }

    public BaseViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, this);
        if (!isAttrs) {
            isAttrs = true;
            onAttrs(context, attrs);
        }
    }

    public BaseViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, this);
        if (!isAttrs) {
            isAttrs = true;
            onAttrs(context, attrs);
        }
    }

    private void init(Context context, BaseViewGroup parent) {
        if (!isInit) {
            mRootView = LayoutInflater.from(context).inflate(onLayoutId(), parent, true);
            isInit = true;
        }
    }

    protected void onAttrs(Context context, AttributeSet attrs) {

    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        onInitChild();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        onInitChild();
    }

    private void onInitChild() {
        if (!isLoadChildView) {
            isLoadChildView = true;
            onFindView(mRootView);
            onBindValue();
            onBindListener();
        }
    }

    protected abstract int onLayoutId();

    protected abstract void onBindListener();

    protected abstract void onBindValue();

    protected abstract void onFindView(View rootView);
}
